package com.heima.wemedia.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.consatnts.admin.AdminConstants;
import com.heima.common.exception.CustException;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import com.heima.model.threadlocal.WmThreadLocalUtils;
import com.heima.model.wemedia.dtos.WmUserDTO;
import com.heima.model.wemedia.pojos.WmUser;
import com.heima.model.wemedia.vos.WmUserVO;
import com.heima.utils.common.AppJwtUtil;
import com.heima.wemedia.mapper.WmUserMapper;
import com.heima.wemedia.service.WmUserService;
import io.swagger.annotations.Api;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;


@Service

public class WmUserServiceImpl extends ServiceImpl<WmUserMapper, WmUser> implements WmUserService {

    @Override
    public ResponseResult login(WmUserDTO dto) {
        String name = dto.getName();
        String password = dto.getPassword();
        if (StringUtils.isBlank(name) || StringUtils.isBlank(password)) {
            CustException.cust(AppHttpCodeEnum.PARAM_INVALID, "用户名和密码不能为空");
        }
        WmUser wmUser = getOne(Wrappers.<WmUser>lambdaQuery().eq(WmUser::getName, dto.getName()));
        if (wmUser == null) {
            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST, "用户名数据不存在");
        }
//        String Reqpassword = DigestUtils.md5DigestAsHex((password + wmUser.getSalt()).getBytes());
//        if (Reqpassword.equals(wmUser.getPassword())) {
//            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR, "密码错误");
//        }
        if (!AdminConstants.USER_STATUS_ALLOW.equals(wmUser.getStatus())) {
            CustException.cust(AppHttpCodeEnum.LOGIN_STATUS_ERROR, "该用户不在等待队列");
        }
        wmUser.setCreatedTime(new Date());
        this.updateById(wmUser);
        String token = AppJwtUtil.getToken(Long.valueOf(wmUser.getId()));
       Map map = new HashMap<>();
       map.put("token",token);
        WmUserVO wmUserVO = new WmUserVO();
        BeanUtils.copyProperties(wmUser,wmUserVO);
        map.put("user",wmUserVO);
        return ResponseResult.okResult(map);

//
//        //获得用户名和密码 判断是否为空
//        String name = dto.getName();
//        String password = dto.getPassword();
//        if(StringUtils.isBlank(name)||StringUtils.isBlank(password)){
//            CustException.cust(AppHttpCodeEnum.PARAM_INVALID);
//        }
//
//        //获取登录的自媒体用户 判断
//        WmUser user = WmThreadLocalUtils.getUser();
//        if(user==null){
//            CustException.cust(AppHttpCodeEnum.DATA_NOT_EXIST);
//        }
//        //md5密码编辑 并判断
//        String md5Password = DigestUtils.md5DigestAsHex((password + user.getSalt()).getBytes());
//        if(!md5Password.equals(user.getPassword())){
//            CustException.cust(AppHttpCodeEnum.LOGIN_PASSWORD_ERROR);
//        }
//        //判断用户状态
//        if(!AdminConstants.USER_STATUS_ALLOW.equals(user.getStatus())){
//            CustException.cust(AppHttpCodeEnum.LOGIN_STATUS_ERROR);
//        }
//        //设置创建时间
//        user.setCreatedTime(new Date());
//        //更新wmUser表
//        this.updateById(user);
//        //获取token 使用AppJwtUtil
//        String token = AppJwtUtil.getToken(Long.valueOf(user.getId()));
//        //用map集合来存储 把token和userVo放进去   userVO是copy user的实体类
//       Map map= new HashMap<>();
//       map.put("token",token);
//        WmUserVO wmUserVO = new WmUserVO();
//        BeanUtils.copyProperties(user,wmUserVO);
//        map.put("user",wmUserVO);
//
//        return ResponseResult.okResult(map);
    }


}